//=============================================================================
// PepperPotRocket
//=============================================================================
class PepperPotRocket extends TFProjectile;

var Emitter Flame;
var class<Emitter> FlameClass;
var class<Emitter> SteamClass;
var xEmitter Smoke;
var class<xEmitter> SmokeClass;

var PepperPotDart Target;

var() float TurnSpeed; //1 -> very fast, 0 -> not a lot.
var() float WobbleScale; //1 -> Insane wobbliness, 0 -> Totally straight.

replication
{
	reliable if(Role == ROLE_Authority)
		Target;
}

simulated function PostBeginPlay()
{
	Super.PostBeginPlay();

	if(Level.NetMode != NM_DedicatedServer)
	{
		if(Flame == None)
		{
			if(PhysicsVolume.bWaterVolume)
				Flame = Spawn(SteamClass, self);
			else
				Flame = Spawn(FlameClass, self);
		}

		Smoke = Spawn(SmokeClass, self);
	}

	Velocity = Speed * Vector(Rotation);
	Acceleration  = Vector(Rotation) * 3000;

	SoundPitch = Default.SoundPitch * (1.8 - FRand());

	SetTimer(0.1, true);
}

//Go slower underwater.
simulated function PhysicsVolumeChange(PhysicsVolume NewVolume)
{
	if(NewVolume.bWaterVolume)
	{
		Speed = Default.Speed * 0.3;
		MaxSpeed = Default.MaxSpeed * 0.3;
		Velocity *= 0.3;
		Acceleration  = Vector(Rotation) * 1000;

		if(Level.NetMode != NM_DedicatedServer)
		{
			if(Flame != None && Flame.Class != SteamClass)
				Flame.Destroy();

			if(Flame == None)
				Flame = Spawn(SteamClass, self);
		}
	}
	else
	{
		Speed = Default.Speed;
		MaxSpeed = Default.MaxSpeed;
		Acceleration  = Vector(Rotation) * 3000;

		if(Level.NetMode != NM_DedicatedServer)
		{
			if(Flame != None && Flame.Class != FlameClass)
			{
				Flame.Emitters[0].RespawnDeadParticles = false;
				Flame.AutoDestroy = true;
				Flame.LifeSpan = 3;
			}

			if(Flame == None)
				Flame = Spawn(FlameClass, self);
		}
	}
}

//Stolen from the SeekingRocketProj!
simulated function Timer()
{
	local vector ForceDir, WobbleAmount;
	local float VelMag;

	//If there is a dart to home in on, do so.
	if(Target != None)
	{
		//Add some randomness to the direction.
		//Rockets spread out wide, then converge on pawns
		//For anything else they spread out to cover a wider area, increasing splash damage.
		//N.B. Home in on pawn's location rather than dart's, because it is more reliable in net games.
		if(Target.P != None)
		{
			WobbleAmount = VSize(Location - Target.P.Location) * VRand() * WobbleScale;
			ForceDir = Normal(Target.P.Location - Location + WobbleAmount);
		}
		else
		{
			WobbleAmount = FMax(2000 - VSize(Location - Target.Location), 500) * VRand() * WobbleScale;
			ForceDir = Normal(Target.Location - Location + WobbleAmount);
		}

		VelMag = VSize(Velocity);
		ForceDir = Normal(ForceDir * TurnSpeed * VelMag + Velocity);

		Velocity =  VelMag * ForceDir;
		Acceleration = 3000 * ForceDir;

		SetRotation(rotator(Velocity));
	}
}

simulated function Landed(vector HitNormal)
{
	HitWall(HitNormal, None);
}

simulated function ProcessTouch (Actor Other, Vector HitLocation)
{
	//Don't hit instigator until rocket has travelled a bit, to prevent stupid suicides.
	if(Other == Instigator && Lifespan > Default.Lifespan - 1)
		return;

	Explode(HitLocation, normal(velocity));
}

simulated function HitWall(vector HitNormal, actor Wall)
{
	Explode(Location, HitNormal);
}

simulated function Explode(vector HitLocation, vector HitNormal)
{
	BlowUp(HitLocation);

	if(PhysicsVolume.bWaterVolume)
		PlaySound(Sound'TFMedia.PepperPot.PepperPotRocketBubble',, 0.4 * TransientSoundVolume);
	else
		PlaySound(Sound'TFMedia.PepperPot.PepperPotRocketBang',, 0.5 * TransientSoundVolume);

	if(EffectIsRelevant(Location,false))
	{
		Spawn(class'PepperPotHitEffect',,, HitLocation + HitNormal*4, rotator(-HitNormal));
		Spawn(ExplosionDecal,self,,HitLocation, rotator(-HitNormal));
	}

	Destroy();
}

function BlowUp(vector HitLocation)
{
	HurtRadius(Damage, DamageRadius, MyDamageType, MomentumTransfer, HitLocation);
	MakeNoise(1.0);
}

simulated function Destroyed()
{
	if(Flame != None)
	{
		Flame.Emitters[0].RespawnDeadParticles = false;
		Flame.AutoDestroy = true;
		Flame.LifeSpan = 3;
	}

	if(Smoke != None)
		Smoke.mRegen = false;

	Super.Destroyed();
}

defaultproperties
{
     FlameClass=Class'taskforces.PepperPotFlame'
     SteamClass=Class'taskforces.PepperPotSteam'
     SmokeClass=Class'taskforces.PepperPotSmoke'
     TurnSpeed=0.600000
     WobbleScale=0.130000
     Speed=300.000000
     TossZ=0.000000
     Damage=10.000000
     DamageRadius=150.000000
     MomentumTransfer=20000.000000
     MyDamageType=Class'taskforces.DamTypePepperPot'
     ExplosionDecal=Class'XEffects.LinkBoltScorch'
     DrawType=DT_StaticMesh
     StaticMesh=StaticMesh'WeaponStaticMesh.RocketProj'
     AmbientSound=Sound'TFMedia.PepperPot.PepperPotRocketAmbient'
     DrawScale=0.400000
     AmbientGlow=16
     bUnlit=False
     SoundVolume=70
     SoundRadius=70.000000
}
